﻿using System;
using System.Collections.Generic;
using System.Linq;
using Backend.JYD.Model;
using Backend.JYD.Model.Cust;

namespace Backend.JYD.EFDAL.DAL
{
    public class CustBaseDAL : Backend.JYD.IDAL.Cust.ICustBase
	{
		#region 查询数据CustBase

		/// <summary>
		/// 查询数据
		/// </summary>
        public PageInfo<CustBase> SearchCustBaseInfo(PageInfo page, string no, string name, string phone, int? chanel, bool isAll)
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<CustBase> query = db.CustBases;
                if (!string.IsNullOrWhiteSpace(no))
                {
                    query = query.Where(p => p.CustNo == no);
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(name))
                    {
                        query = query.Where(p => p.CustName.Contains(name));
                    }
                    if (!string.IsNullOrWhiteSpace(phone))
                    {
                        query = query.Where(p => p.Phone == phone);
                    }
                    if (chanel.HasValue && chanel > 0)
                    {
                        query = query.Where(p => p.ChanelType == chanel.Value);
                    }
                    if (isAll)
                    {
                        query = query.Where(p => p.Status > 0);
                    }
                }
                //其他条件代码
                return new PageInfo<CustBase>(page, query);
            }
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        public IList<CustBase> GetCustBaseByCustNos(IList<string> custNos)
        {
            string[] nos = custNos.ToArray();
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<CustBase> query = db.CustBases.Where(p => nos.Contains(p.CustNo));
                //其他条件代码
                return query.ToList();
            }
        }

        public CustBase GetCustBaseByCustNo(string custNo)
        {
            using (JydDataContext db = new JydDataContext())
            {
                 return db.CustBases.Where(p => p.CustNo == custNo).FirstOrDefault();
            }
        }

        public CustBase GetCustBaseByPhone(string phone)
        {
            using (JydDataContext db = new JydDataContext())
            {
                return db.CustBases.Where(p => p.Phone == phone).FirstOrDefault();
            }
        }

        public CustBase GetCustBaseByPaper(string paperType, string paperNo, string custName)
        {
            using (JydDataContext db = new JydDataContext())
            {
                return db.CustBases.Where(p =>p.CustName == custName && p.PaperType == paperType && p.PaperNo == paperNo).FirstOrDefault();
            }
        }

		#endregion

		#region 插入数据

		/// <summary>
		/// 插入数据CustBase
		/// </summary>
		public string AddCustBase(CustBase info)
		{
			using (JydDataContext db = new JydDataContext())
			{
				db.CustBases.Add(info);
				db.SaveChanges();
				return info.CustNo;
			}
		}
		#endregion

		#region 修改数据CustBase

        public bool UpdateCustBase(CustBase info)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.CustBases.Where(p => p.CustNo == info.CustNo);
                if (query != null)
                {
                    var temp = query.FirstOrDefault();
                    if (temp != null)
                    {
                        temp.OpId = info.OpId;
                        temp.UpdateTime = DateTime.Now;
                        temp.Status = info.Status;
                        temp.Type = info.Type;
                        temp.Phone = info.Phone;
                        temp.CustName = info.CustName;
                        temp.PaperNo = info.PaperNo;
                        temp.PaperType = info.PaperType;

                        return db.SaveChanges() > 0;
                    }
                }
                return false;
            }
        }

        public bool UpdateCustBaseInfo(CustBase info)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.CustBases.Where(p => p.CustNo == info.CustNo);
                if (query != null)
                {
                    var temp = query.FirstOrDefault();
                    if (temp != null)
                    {
                        temp.OpId = info.OpId;
                        temp.UpdateTime = DateTime.Now;
                        temp.Status = info.Status;
                        temp.Type = info.Type;
                        temp.Phone = info.Phone;

                        return db.SaveChanges() > 0;
                    }
                }
                return false;
            }
        }

		#endregion
    }
}